package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.TransactionType;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class TransTypeBean extends AbstractBean {
	public static final String BEAN_NAME="transTypeBean";
	private MasterSetupService service;
	private List<TransactionType> transTypeList;
	private TransactionType transType;
	public static final String NAV_LIST_TRANSACTIONTYPE="listTransType";
	public static final String NAV_ADD_TRANSACTIONTYPE="addTransType";
	public static final String NAV_EDIT_TRANSACTIONTYPE="editTransType";
	 
	public MasterSetupService getService() {
		return service;
	}
	public void setService(MasterSetupService service) {
		this.service = service;
	}
	public TransactionType getTransType() {
		return transType;
	}
	public void setTransType(TransactionType transType) {
		this.transType = transType;
	}
	public List<TransactionType> getTransTypeList() {
		try{
			transTypeList = service.getTransactionTypeList();
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return transTypeList;
	}
	public void setTransTypeList(List<TransactionType> transTypeList) {
		this.transTypeList = transTypeList;
	}
	
	public String navCreationAction(){
		transType = new TransactionType();
		return NAV_ADD_TRANSACTIONTYPE;
	}
	
	public String createAction(){
		try{
			transType.setCreatedBy(getThisUser());
			transType.setCreatedDate(new Date());
			transType.setIsDeleted(CommonConstant.NOT_DELETED);
			service.saveTransactionType(transType);
			return NAV_LIST_TRANSACTIONTYPE;
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_ADD_TRANSACTIONTYPE;
	}
	
	public void editListener(ActionEvent actionEvent){
		transType = null;
		UIParameter parameter = (UIParameter)actionEvent.getComponent().findComponent("transTypeId");
		String transTypeId = (String)parameter.getValue();
		for(TransactionType a:transTypeList){
			if(a.getTransactionTypeId().equalsIgnoreCase(transTypeId)){
				transType = a;
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT_TRANSACTIONTYPE;
	}
	
	public String editAction(){
		try{
			transType.setModifiedBy(getThisUser());
			transType.setModifiedDate(new Date());
			service.updateTransactionType(transType);
			return NAV_LIST_TRANSACTIONTYPE;
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		
		return NAV_EDIT_TRANSACTIONTYPE;
	}
	
	public String deleteAction(){
		try{
			
			List<TransactionType> deleteList = new ArrayList<TransactionType>();
			for(TransactionType a:transTypeList){
				if(a.isMarkAsAction()){
					deleteList.add(a);
				}
			}
			service.deleteTransactionTypes(deleteList);
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_LIST_TRANSACTIONTYPE;
	}
}
